<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class shopControl extends skymvc{
		public $userid;
		public $skins="shop/index/";
		public $shopdata;
		public function __construct(){
			parent::__construct();
		}
		
		public function onInit(){
			$this->userid=M("login")->userid;
			
			$shopid=get_post("shopid","i");
			$this->shopdata=M("shop")->selectRow(array("where"=>" status<10 AND shopid=".$shopid));
			if(empty($this->shopdata)){
				$this->goall("店铺不存在或者被禁止了",1,0,"/index.php");
			}
			$this->shopdata['isfav']=0;
			if($this->userid){
				$fav=M("fav")->selectRow("tablename='shop' AND object_id=".$shopid." AND userid=".$this->userid);
				if($fav){
					$this->shopdata['isfav']=1;
				}
			}
			$this->skins="shop/".$this->shopdata['tpl'];
			$this->smarty->assign(
				array(
				"shopskins"=>"shop/".$this->shopdata['tpl'],
				"shop"=>$this->shopdata
				)
			);
		}
		
		public function onDefault(){
			 
			$shopid=get_post("shopid","i");
			$data=$this->shopdata;
			if(empty($data)){
				$this->goall($this->lang['shop_no_exists'],1,0,"/index.php");
			}
			$where=" status=2 AND shopid=".$shopid;
			$baseurl=$url="/index.php?m=shop&shopid=".$shopid;
			$shop_cid=get('shop_cid','i');
			$keyword=get('keyword','h');
			if($shop_cid){
				$where.=" AND shop_cid=".$shop_cid;
				$baseurl.="&shop_cid=".$shop_cid;
			}else{
				if(!$keyword){
					$where.=" AND is_recommend=1 ";
				}
			}
			$url=$baseurl;
			
			if($keyword){
				$where.=" AND title like '%".$keyword."%' ";
				$url.="&keyword=".urlencode($keyword);
			}
			
			$index='';
			switch(get('index')){
				case 'asc':
						$index='asc';
					break;
				case 'desc':
						$index='desc';
					break;
			}
			switch(get('orderby')){
				case "price":
					$order=" price $index";
					break;
				case "buy_num":
					$order=" buy_num $index";
					break;
				default:
					$order=" id DESC";
					break;
			}
			$url.="&index=".get('index','h')."&orderby=".get('orderby','h');
			$start=get('per_page','i');
			$limit=48;
			$option=array(
				"where"=>$where,
				"order"=>$order,
				"start"=>$start,
				"limit"=>$limit
			);
			$rscount=true;
			$prolist=M("shop_product")->select($option,$rscount);
			$cart=M("shop_order_cart")->getListByUser();
			if($prolist){
				foreach($prolist as $k=>$v){
					if(isset($cart['product'][$v['id']])){
						$v['incart']=1;
						$v['cart']=$cart['product'][$v['id']];
					}else{
						$v['incart']=0;
					}
					$prolist[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$sdata=array(
				"shop"=>$data,
				"prolist"=>$prolist,
				"pagelist"=>$pagelist,
				"seo"=>array(
					"title"=>$data['shopname'],
					"keywords"=>$data['shopname'],
					"description"=>$data['info']
				),
				"per_page"=>$per_page,
				"baseurl"=>$baseurl,
				"cart"=>$cart,
				"catlist"=>M("shop_category")->children(array("where"=>"  pid=0 AND shopid=".$shopid))
				
			);
			 
			 
			$this->smarty->goassign($sdata);
 
			 
			$this->smarty->display($this->skins."index.html");
		}
		
		public function onDetail(){
			$shopid=get_post("shopid","i");
			$data=$this->shopdata;
			$data['content']=M("shop_data")->selectOne(array("where"=>"shopid=".$shopid,"fields"=>"content"));
			$sdata=array(
				"shop"=>$data,
				"seo"=>array(
					"title"=>$data['shopname'],
					"keywords"=>$data['shopname'],
					"description"=>$data['info']
				),
			);
			if(get('ajax')){
				$this->goAll("success",0,$sdata);
			}
			$this->smarty->assign($sdata);			 
			$this->smarty->display($this->skins."detail.html");
		}
		
		public function onShow(){
			$shopid=get_post("shopid","i");
			$data=$this->shopdata;
			if(empty($data)){
				$this->goall($this->lang['shop_no_exists'],1,0,"/index.php");
			}
			  
			
			 
			$this->smarty->assign(array(
				"shop"=>$data,
				"seo"=>array(
					"title"=>$data['shopname'],
					"keywords"=>$data['shopname'],
					"description"=>$data['info']
				),
				
			));
			$tpl="index/";
			if($data['tpl']){
				$tpl=$data['tpl']."/";
			}
			 
			$this->smarty->display($this->skins."show.html");
		}
		
		public function onComment(){
			$shopid=get_post("shopid","i");
			$data=$this->shopdata;
			if(empty($data)){
				$this->goall($this->lang['shop_no_exists'],1,0,"/index.php");
			}
			$url="/index.php?m=shop&a=comment&shopid=".$shopid;
			$where=" shopid=".$shopid;
			switch(get('t')){
				case "good":
					$where.=" AND grade>=3";
					break;
				case "bad":
					$where.=" AND grade<3";
					break;
			}
			$url.="&t=".get('t','h');
			$limit=30;
			$start=get('per_page','i');
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>"id DESC"
			);

			$rscount=true;  
			$comment_list=M("shop_order_comment")->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			if($comment_list){
				foreach($comment_list as $v){
					$uids[]=$v['userid'];
					
				}
				$us=M("user")->getUserByIds($uids);
				foreach($comment_list as $k=>$v){
					$u=$us[$v['userid']];
					$v['nickname']=$u['nickname'];
					$v['user_head']=$u['user_head'];
					$comment_list[$k]=$v;
				}
			}
			$this->smarty->goassign(array(
				"shop"=>$data,
				"comment_list"=>$comment_list,
				"seo"=>array(
					"title"=>$data['shopname'],
					"keywords"=>$data['shopname'],
					"description"=>$data['info']
				),
				"pagelist"=>$pagelist
				
			));
			$tpl="index/";
			if($data['tpl']){
				$tpl=$data['tpl']."/";
			}
			 
			$this->smarty->display($this->skins."comment.html");
		}
		
		public function onForAddLove(){
			$this->loadControl("shopapi");
			//已收藏的
			$favids=$this->shopapiControl->favshopids(SC_ID);
			//该区域的
			$scids=M("site_city")->id_family($sc_id);
			$where=" status=1 ";
			if($scids){
				$shopids=M("shop")->selectCols(array("where"=>" sc_id in("._implode($scids).") ","fields"=>"shopid"));
				if($favids){
					$ids=array_diff($shopids,$favids);
				}else{
					$ids=$shopids;
				}
				if($ids){
					 $where.=" AND shopid in("._implode($ids).")";
				}else{
					$where.=" AND 1=2 ";
				}
			}else{
				$where.=" AND 1=2 ";
			}
			$data=M("shop")->select(array("where"=>$where,"limit"=>8));
			$this->smarty->assign(array(
				"data"=>$data
			));
			$this->smarty->display("shop/foraddlove.html");
		}
		
		public function onMyLove(){
			$where=" tablename='shop'  ";
			$option=array(
				"where"=>$where,
				"fields"=>"object_id",
			);
			$rscount=true;
			$ids=M("love")->selectCols($option,$rscount);
			if($ids){
				$shoplist=M("shop")->select(array("where"=>" shopid in("._implode($ids).") " ));
				foreach($shoplist as $k=>$v){
					if($type=M("shop")->working($v['open_time'])=="doing"){
						$v['is_work']=1;
					}
					$shoplist[$k]=$v;
				}
				$this->smarty->assign(array(
					"shoplist"=>$shoplist,
					"rscount"=>$rscount
				));
			}
			$this->smarty->display("user/love_shop.html");
		}
		
		public function onAdd(){
			$shopid=get_post("shopid","i");
			if($shopid){
				$data=$this->shopdata;
				if($data['sc_id']){
					$t_3=M("site_city")->selectRow(array("where"=>" sc_id=".$data['sc_id']));
					if($t_3['pid']){
						$t_2=M("site_city")->selectRow(array("where"=>" sc_id=".$t_3['pid']));
						
						if($t_2['pid']){
							$t_1=M("site_city")->selectRow(array("where"=>" sc_id=".$t_2['pid']));
							$sc_2nd_list=M("site_city")->select(array("where"=>" pid=".$t_2['pid']));							
							$sc_3nd_list=M("site_city")->select(array("where"=>" pid=".$t_2['sc_id']));
							$sc_top_id=$t_1['sc_id'];
							$sc_2nd_id=$t_2['sc_id'];
							$sc_3nd_id=$t_3['sc_id'];
						}else{
							$sc_2nd_list=M("site_city")->select(array("where"=>" pid=".$t_2['pid']));
							$sc_top_id=$t_2['sc_id'];
							$sc_2nd_id=$t_3['sc_id'];
						}
					}else{
						$sc_top_id=$t_1['sc_id'];
					}
				}
			}
			$this->smarty->assign(array(
				"data"=>$data,
				"sc_top_list"=>$sc_top_list,
				"sc_2nd_list"=>$sc_2nd_list,
				"sc_3nd_list"=>$sc_3nd_list,
				"sc_top_id"=>$sc_top_id,
				"sc_2nd_id"=>$sc_2nd_id,
				"sc_3nd_id"=>$sc_3nd_id,
			));
			$this->smarty->display("shop/add.html");
		}
		
		public function onApply(){
			M("login")->checkLogin();
			
			$this->smarty->display("shop/apply.html");
		}
		
		public function onSave(){
			
			$shopid=get_post("shopid","i");
			$data["userid"]=M("login")->userid;
			$data["word"]=post("word","h");
			$data["shopname"]=post("shopname","h");
			if(empty($data['shopname'])) $this->goall($this->lang['shopname_is_empty'],1);
			if(!$shopid){
				if(M("shop")->selectRow(array("where"=>"userid=".$data['userid']))){
					$this->goall($this->lang['shop_is_applyed'],1,0,"/index.php");
				}
				$data["dateline"]=time();
				
			}
			$data["sc_id"]=array_lastnum(get_post("sc_id","i"));
			$data['address']=post('address','h');
			$data['telephone']=post('telephone','h');
			$data['info']=post('info','h'); 
			$data["shopurl"]=post("shopurl","h");
			$data["shop_uid"]=post("shop_uid","i");
			$data["nickname"]=post("nickname","h");
			$data["goods_num"]=post("goods_num","i");
			$data["sold_num"]=post("sold_num","i");
			$data["rank"]=post("rank","i");
			$data["ismall"]=post("ismall","i");
			$data["goodsgrade"]=post("goodsgrade","r",2);
			
			$data["last_time"]=post("last_time","i");
			$data["logo"]=post("logo","h");
			$data["is_invite"]=post("is_invite","i");

			if($shopid){
				M("shop")->update($data,"shopid='$shopid'");
				$sdata['content']=post('content');
				M("shop_data")->update($sdata,"shopid='$shopid'");
			}else{
				$shopid=M("shop")->insert($data);
				$sdata['shopid']=$shopid;
				$sdata['dateline']=time();
				
				M("shop_data")->insert($sdata);
				//绑定用户
				M("user")->update(array("shopid"=>$shopid),"userid=".M("login")->userid);
			}
			if(post('isapply')){
				$this->goall($this->lang['shop_apply_success'],1,0,"/index.php");
			}else{
				$this->goall($this->lang['save_success'],0,$data);
			}
		}
		
 
		
		
		
		
	}
 
?>